package com.lixianbiao.validation.handler;

import com.lixianbiao.validation.annotation.Max;
import com.lixianbiao.validation.annotation.Min;
import com.lixianbiao.validation.compoent.Handler;
import com.lixianbiao.validation.compoent.Message;
import com.lixianbiao.validation.annotation.NotNull;
import com.lixianbiao.validation.annotation.StringPattern;
import com.lixianbiao.validation.processor.MaxProcessor;
import com.lixianbiao.validation.processor.MinProcessor;
import com.lixianbiao.validation.processor.NotNullProcessor;
import com.lixianbiao.validation.processor.PatternProcessor;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;

/**
 * @author lixianbiao
 * @since 2024/06/22 09:54
 */

// TODO 注解存在顺序依赖问题;类嵌套检测；同一个值在不同方法中检测逻辑不同
public class ProcessHandler extends Handler {

    // 值合法性检验：扫描所有属性，如果被自定义注解注解了，获取对应处理器，对属性值进行判断
    @Override
    public void doHandler(Object member, Message message) throws IllegalAccessException {
        Class<?> memberClazz = member.getClass();
        Field[] declaredFields = memberClazz.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            Annotation annotation = null;
            declaredField.setAccessible(true);
            Object value = declaredField.get(member);
            // TODO 初始化好Map<annotation.class,processor>，然后遍历map找出对应的processor，无需重复创建processor
            if((annotation = declaredField.getAnnotation(NotNull.class)) != null){
                NotNullProcessor processor = new NotNullProcessor();
                if(!processor.process(annotation,value)){
                    message.result = false;
                    message.code = ((NotNull)annotation).code();
                    message.message = ((NotNull)annotation).message();
                    return;
                }
            }
            if((annotation = declaredField.getAnnotation(StringPattern.class)) != null){
                PatternProcessor patternProcessor = new PatternProcessor();
                if(!patternProcessor.process(annotation,value)){
                    message.result = false;
                    message.code = ((StringPattern)annotation).code();
                    message.message = ((StringPattern)annotation).message();
                    return;
                }
            }

            if((annotation = declaredField.getAnnotation(Max.class)) != null){
                MaxProcessor maxProcessor = new MaxProcessor();
                if(!maxProcessor.process(annotation,value)){
                    message.result = false;
                    message.code = ((Max)annotation).code();
                    message.message = ((Max)annotation).message();
                }
            }

            if((annotation = declaredField.getAnnotation(Min.class)) != null){
                MinProcessor minProcessor = new MinProcessor();
                if(!minProcessor.process(annotation,value)){
                    message.result = false;
                    message.code = ((Min)annotation).code();
                    message.message = ((Min)annotation).message();
                }
            }
        }
        if(next != null){
            next.doHandler(member,message);
        }
    }
}